package grimbo.goodwinfooty;

import grimbo.goodwinfooty.processing.CSV2UntypedListProcessor;
import grimbo.goodwinfooty.processing.Processor;
import grimbo.goodwinfooty.processing.XLS2CSVProcessor;
import grimbo.goodwinfooty.processing.XLS2InputStreamProcessor;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

/**
 * Not named ending in "Test" because it cannot be executed as standalone test
 * due to reliance on xlsFile being setup by a TestSuite.
 */
public class CSVContentsTest_ extends TestCase {
    private static Map context;
    private static List processors;

    private File xlsFile;

    protected void setUp() throws Exception {
        super.setUp();
        if (null == processors) {
            processors = new ArrayList();
            processors.add(new XLS2InputStreamProcessor());
            processors.add(new XLS2CSVProcessor());
            processors.add(new CSV2UntypedListProcessor());
            context = new HashMap();
            String filename = xlsFile.getCanonicalPath();
            context.put(XLS2InputStreamProcessor.CTX_XLS_FILE_NAME, filename);
            context = generate(processors, context);
        }
    }

    private Map generate(List processors, Map context) throws Exception {
        for (int i = 0; i < processors.size(); i++) {
            Processor p = (Processor) processors.get(i);
            p.process(context);
        }
        return context;
    }

    public void testNameColumnsAreEqual() {
        final int FIRST_NAME_COLUMN = 0;
        // difference between last column and second name column
        final int SECOND_NAME_COLUMN_DIFF = 9;
        List untypedList = (List) context.get(CSV2UntypedListProcessor.CTX_UNTYPED_LIST);
        int index = 0;
        for (Iterator it = untypedList.iterator(); it.hasNext();) {
            String[] values = (String[]) it.next();
            String name1 = values[FIRST_NAME_COLUMN];
            String name2 = values[values.length - SECOND_NAME_COLUMN_DIFF];
            assertEquals(name1, name2);
        }
    }

    public File getXlsFile() {
        return xlsFile;
    }

    public void setXlsFile(File xlsFile) {
        this.xlsFile = xlsFile;
    }
}
